Skip to content

BaseIPythonWidget

Sub classes

Name Children Inherits
InProcessIPythonWidget
prettyqt.ipython.inprocessipythonwidget
Convenience class for a live IPython console widget.
    OutOfProcessIPythonWidget
    prettyqt.ipython.outofprocessipythonwidget
    Convenience class for a live IPython console widget running out-of-process.

      Base classes

      Name Children Inherits
      RichJupyterWidget
      qtconsole.rich_jupyter_widget
      An JupyterWidget that supports rich text, including lists, images, and
      WidgetMixin
      prettyqt.widgets.widget

      â‹” Inheritance diagram

      graph TD
        412360768["ipython.BaseIPythonWidget"]
        548562404["rich_jupyter_widget.RichJupyterWidget"]
        3893578720["rich_jupyter_widget.RichIPythonWidget"]
        2577579137["jupyter_widget.JupyterWidget"]
        1359188613["jupyter_widget.IPythonWidget"]
        1600289359["frontend_widget.FrontendWidget"]
        4247442520["history_console_widget.HistoryConsoleWidget"]
        330948184["console_widget.ConsoleWidget"]
        831903233["util.NewBase"]
        3806155832["configurable.LoggingConfigurable"]
        1666290838["configurable.Configurable"]
        2478326197["traitlets.HasTraits"]
        2674025753["traitlets.HasDescriptors"]
        2751680898["builtins.object"]
        1089529877["util.SuperQClass"]
        3620195106["QtWidgets.QWidget"]
        3984060476["QtCore.QObject"]
        37984305["Shiboken.Object"]
        673956333["QtGui.QPaintDevice"]
        1685905456["base_frontend_mixin.BaseFrontendMixin"]
        829357528["widgets.WidgetMixin"]
        3227531497["core.ObjectMixin"]
        625385617["gui.PaintDeviceMixin"]
        548562404 --> 412360768
        3893578720 --> 548562404
        2577579137 --> 3893578720
        1359188613 --> 2577579137
        1600289359 --> 1359188613
        4247442520 --> 1600289359
        330948184 --> 4247442520
        831903233 --> 330948184
        3806155832 --> 831903233
        1666290838 --> 3806155832
        2478326197 --> 1666290838
        2674025753 --> 2478326197
        2751680898 --> 2674025753
        1089529877 --> 831903233
        3620195106 --> 1089529877
        3984060476 --> 3620195106
        37984305 --> 3984060476
        2751680898 --> 37984305
        673956333 --> 3620195106
        37984305 --> 673956333
        1685905456 --> 1600289359
        2751680898 --> 1685905456
        829357528 --> 412360768
        3227531497 --> 829357528
        2751680898 --> 3227531497
        625385617 --> 829357528
        2751680898 --> 625385617
        2751680898 --> 829357528

      🛈 DocStrings

      Bases: RichJupyterWidget, WidgetMixin

      Convenience class for a live IPython console widget running out-of-process.

      Source code in prettyqt/ipython/baseipythonwidget.py
      class BaseIPythonWidget(RichJupyterWidget, widgets.WidgetMixin):
          """Convenience class for a live IPython console widget running out-of-process."""
      
          code_executed = core.Signal(str)
      
          def __init__(self, *args, **kwargs):
              kwargs.setdefault("gui_completion", "droplist")
              kwargs.setdefault("kind", "rich")
              kwargs.setdefault("paging", "vsplit")
              super().__init__(*args, **kwargs)
              self.banner = "IPython Console"
              self.font_size = 6
              # self.gui_completion = "droplist"
              widgets.Application.call_on_exit(self.shutdown)
              # self.exit_requested.connect(self.shutdown)
      
              widgets.Application.styleHints().colorSchemeChanged.connect(
                  self.adjust_style_to_palette
              )
              self.adjust_style_to_palette()
      
          @classmethod
          def setup_example(cls):
              return None
      
          def set_completion_mode(self, mode: CompletionmodeStr):
              self._set_completion_widget(mode)
      
          def get_completion_mode(self) -> CompletionmodeStr:
              return self.gui_completion  # type: ignore
      
          @property
          def texteditor(self) -> widgets.QPlainTextEdit:  # to shut up linters
              return self._control  # type: ignore
      
          def adjust_style_to_palette(self):
              """Adjust coloring of the terminal to current palette."""
              pal = widgets.Application.get_palette()
              style = "linux" if pal.is_dark() else "lightbg"
              self.set_default_style(style)
      
          def shutdown(self):
              """Stop IPython and cleanup."""
              return NotImplemented
      
          def buffer(self) -> str:
              """Get current code block."""
              return self.input_buffer
      
          def set_buffer(self, code: str) -> None:
              """Set code string to Jupyter QtConsole buffer."""
              self.input_buffer = ""
              if not isinstance(code, str):
                  msg = f"Cannot set {type(code)}."
                  raise ValueError(msg)  # noqa: TRY004
              cursor = self.texteditor.textCursor()
              lines = code.split("\n")
              for line in lines[:-1]:
                  cursor.insertText(line + "\n")
                  self._insert_continuation_prompt(cursor)  # insert "...:"
              cursor.insertText(lines[-1])
      
          def insert_text(self, text: str) -> None:
              cursor = self.texteditor.textCursor()
              cursor.insertText(text)
      
          def set_temp_text(self, text: str) -> None:
              cursor = self.texteditor.textCursor()
              cursor.removeSelectedText()
              pos = cursor.position()
              cursor.insertText(text)
              cursor.setPosition(pos)
              cursor.setPosition(pos + len(text), gui.TextCursor.MoveMode.KeepAnchor)
              self.texteditor.setTextCursor(cursor)
      
          def get_selected_text(self) -> str:
              """Return the selected text."""
              cursor = self.texteditor.textCursor()
              return cursor.selection().toPlainText()
      
          def clear(self):
              """Clear the terminal."""
              self.texteditor.clear()
      
          def print_text(self, text: str, before_prompt: bool = False):
              """Print some plain text to the console."""
              self._append_plain_text(text, before_prompt=before_prompt)
      
          def execute(
              self,
              source: str | None = None,
              hidden: bool = False,
              interactive: bool = False,
          ):
              """Execute a command in the frame of the console widget."""
              if source is None:
                  source = self.input_buffer
              super().execute(source=source, hidden=hidden, interactive=interactive)
              self.code_executed.emit(source)
      
          completion_mode = core.Property(
              str,
              get_completion_mode,
              set_completion_mode,
              doc="Completion mode for the widget",
          )
      

      adjust_style_to_palette()

      Adjust coloring of the terminal to current palette.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def adjust_style_to_palette(self):
          """Adjust coloring of the terminal to current palette."""
          pal = widgets.Application.get_palette()
          style = "linux" if pal.is_dark() else "lightbg"
          self.set_default_style(style)
      

      buffer() -> str

      Get current code block.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def buffer(self) -> str:
          """Get current code block."""
          return self.input_buffer
      

      clear()

      Clear the terminal.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def clear(self):
          """Clear the terminal."""
          self.texteditor.clear()
      

      execute(source: str | None = None, hidden: bool = False, interactive: bool = False)

      Execute a command in the frame of the console widget.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def execute(
          self,
          source: str | None = None,
          hidden: bool = False,
          interactive: bool = False,
      ):
          """Execute a command in the frame of the console widget."""
          if source is None:
              source = self.input_buffer
          super().execute(source=source, hidden=hidden, interactive=interactive)
          self.code_executed.emit(source)
      

      get_selected_text() -> str

      Return the selected text.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def get_selected_text(self) -> str:
          """Return the selected text."""
          cursor = self.texteditor.textCursor()
          return cursor.selection().toPlainText()
      

      print_text(text: str, before_prompt: bool = False)

      Print some plain text to the console.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def print_text(self, text: str, before_prompt: bool = False):
          """Print some plain text to the console."""
          self._append_plain_text(text, before_prompt=before_prompt)
      

      set_buffer(code: str) -> None

      Set code string to Jupyter QtConsole buffer.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def set_buffer(self, code: str) -> None:
          """Set code string to Jupyter QtConsole buffer."""
          self.input_buffer = ""
          if not isinstance(code, str):
              msg = f"Cannot set {type(code)}."
              raise ValueError(msg)  # noqa: TRY004
          cursor = self.texteditor.textCursor()
          lines = code.split("\n")
          for line in lines[:-1]:
              cursor.insertText(line + "\n")
              self._insert_continuation_prompt(cursor)  # insert "...:"
          cursor.insertText(lines[-1])
      

      shutdown()

      Stop IPython and cleanup.

      Source code in prettyqt/ipython/baseipythonwidget.py
      def shutdown(self):
          """Stop IPython and cleanup."""
          return NotImplemented
      

      :fa-brands-github: Show source on GitHub